package org.programmers.anonymous.fightrobots.service;

import org.programmers.anonymous.fightrobots.controller.RobotController;
import org.programmers.anonymous.fightrobots.model.IRobot;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/robot-service")
public class RobotService implements BeanFactoryAware{
	
	private RobotController robotController = null;
	

	@RequestMapping(value="/getRobot", method = RequestMethod.GET)
	public @ResponseBody IRobot getRobotById(@RequestParam(value="robotId", required=true) String robotId)
	{
		return robotController.loadRobot(Integer.valueOf(robotId));
	}
	

	
	/**
	* Handles request for adding a weapon
	*/
	@RequestMapping(value = "/createRobot", method = RequestMethod.POST)
	public  void createRobot(
	@RequestParam(value = "robotName", required = true) String robotName,
	@RequestParam(value = "robotLife", required = true) Integer robotLife,
	@RequestParam(value = "robotAgressivity", required = true) Integer robotAgressivity,
	@RequestParam(value = "robotDeffense", required = true) String robotDeffense,
	@RequestParam(value = "robotWeapon", required = true) String robotWeapon,
	ModelMap model) {
		IRobot newRobot = robotController.createRobot(robotName, robotLife, robotAgressivity);
		robotController.addDeffense(robotDeffense, newRobot);
		robotController.addWeapon(robotWeapon, newRobot);
		robotController.addRobot(newRobot);
	
	}
	
	
	@Override
	public void setBeanFactory(BeanFactory context) throws BeansException
	{
		robotController = (RobotController) context.getBean("robotController");
		
	}
}